package com.itheima.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.UUID;

@Controller
public class PayController {

    //应用的APPID
    private final String APP_ID = "2021000121669545";
    //生成的应用私钥
//    private final String APP_PRIVATE_KEY = "";
    private final String APP_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCilXQYGgGyF4wyLt0UXZ3yFWU/xVJVqRwHBk8gQqmXBVn6gnmmGKCdRIOqUN1l1fHvhGrjhKjCgIo715+YHvIQuOWVRHI1N4REa2cqXHfs0Wuc/BLgTlZ2FPhs5y+BljjVUja7l5pe3Gv5xRWesVflNn20VjcF6DIimMkRISJgVtHJ2PFv/UbyZGU88O8/IFcQkjrQlwFFdffxuvof7yDPlXfd6mHZhsJft87Nm2YQvtkTNN5QYglpAYFiqryGIURA+kQ6RAHhfRUo0HcEPAeWBVT0Re+5P518uqcE0RND1DSeh9jf8oonBjlLhkUIjPje7DBa0cAOnR5jW1TItzydAgMBAAECggEAbklDPzYlAFHI4CG6Bh6bDaRF0kRf3YuhX4KAGjoeIDuZFb18F5eNHCFlzxqblIDUZM1GVJ+gepAHNSDHOcGEIqPJ2L9dWYwXRPI2QR4Im7RTc2HqzA1XyTHgcr9AEn98ffgO+KGWLdMHy55NVCCopuuXycTR/kfL+nv5qIz5bK9hkJazK3ZmP6Jm3E936Lbu1WR0ExXIlqTf0/7VYHGHzNjhimR0pArjlqklnHnBfNSScXa6uVWktJmXSPT/WMIEQPb8eYP+NLbGZHJx0vqgKwVXltMLaQxdhlfC6zQRUyR244p9yqtbnv/otSzoSsbSU7ra+312dmGHxLjHybUxAQKBgQDkA6fO3O4ZHDu4q5M/bSKlWxJUkS/WobLIpKMmO+m4AmMOcdhmqnGeeYKJ0X+q9LnOcqEr7yaL0Bdv2KBAJfUAjCKT1E9dwzty1++Gx+p+VQoOlY6llSL63h/hBAiz5V8yazm6VbQOnZJP/YIx7JFtpQqbXgT+kWAQJyncn0UzdQKBgQC2ie71sRYr6bvqiWfjvOYEj3l8zPWWdZ3sroje57Z+UxzcezEswA+00gDF/SvpmaiKhf0rH6zr3won0kwJ9KRtVbGDbtgUnLsBYbwYDaA/jv0x2IExYQPb3dNWdI63IhMaM6puk5bZ0xhgOR1hbUBupjQNGKsdaznXbJuJvtyHiQKBgDbKOhSXOg+GnUMu118yVlTtoWX4zUoB6PgVt66ETxEMYckiyiFBa7Ps9tUvJkJqNAvLt7ba7KRECJ2zrC498LWE9voW1kttkmdyQ/hbKZnZzWTbwz8+qv0lSvvDAGRaBZoT8fc4iN/KNVJLVHGyqu9x7S6QVRrmy/KVtpVxICvRAoGBAJPNuEoE7Bj+l4RfYlnLOMz0h8CADVOZRRjCcl1bbi93iOUWc0jy80TMzoeCai4ql4gi2k4yxcfvv79izuM4znMhci0kcZ6vEdUamD05u0n7tBTKZEHhdkhvo5JbqruqwaDzuMr0gq85+IDhfmLHHeb6eScplFSr4BgI/yBYBSVRAoGBAL55Tjip7s6WV58TTPFqDxe9ls4TxZC7V/Esr0WnruieVuqjsJLtaLvu51qz8FjeCq3IEDepJi/I+KyNHLxTshpg80xwP6yow0TtDGlVzB3/33SvgkellDV+taLsP3YaPVx4kboq13k8yAmyx8QbGecJcz4wu12d5yCyx4hcEoDG";
    private final String CHARSET = "UTF-8";
    //支付宝公钥
//    private final String ALIPAY_PUBLIC_KEY = "";
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAopV0GBoBsheMMi7dFF2d8hVlP8VSVakcBwZPIEKplwVZ+oJ5phignUSDqlDdZdXx74Rq44SowoCKO9efmB7yELjllURyNTeERGtnKlx37NFrnPwS4E5WdhT4bOcvgZY41VI2u5eaXtxr+cUVnrFX5TZ9tFY3BegyIpjJESEiYFbRydjxb/1G8mRlPPDvPyBXEJI60JcBRXX38br6H+8gz5V33eph2YbCX7fOzZtmEL7ZEzTeUGIJaQGBYqq8hiFEQPpEOkQB4X0VKNB3BDwHlgVU9EXvuT+dfLqnBNETQ9Q0nofY3/KKJwY5S4ZFCIz43uwwWtHADp0eY1tUyLc8nQIDAQAB";

    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";

  //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
//  private final String NOTIFY_URL = "http://127.0.0.1/notifyUrl";

    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
//    private final String RETURN_URL = “http://localhost:8080/returnUrl”;
//    private final String RETURN_URL = "http://localhost:8080/returnUrl";
    private final String RETURN_URL = "http://localhost:8080/backend/index.html";





    //测试


    @ResponseBody
    @RequestMapping("aliapy")
    public void alipay(HttpServletResponse httpResponse) throws IOException {
        SecureRandom r= new SecureRandom();
        //实例化客户端,填入所需参数
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl(RETURN_URL);
//        request.setNotifyUrl(NOTIFY_URL);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        //生成随机Id
        String out_trade_no = UUID.randomUUID().toString();
        //付款金额，必填
//        String total_amount =Integer.toString(r.nextInt(9999999)+1000000);
        String total_amount =Integer.toString(50000);
        //订单名称，必填
        String subject ="奥迪A8 2021款 A8L 60 TFSl quattro豪华型";
        //商品描述，可空
        String body = "尊敬的会员欢迎购买奥迪A8 2021款 A8L 60 TFSl quattro豪华型";
        request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }



}